{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Predict script for Split model, predict the D and R matrices, and visualize the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dataset.dataset import ImageDataset\n",
    "from modules.split_modules import SplitModel\n",
    "import json\n",
    "from PIL import Image\n",
    "import torch\n",
    "from torchsummary import summary\n",
    "import numpy as np\n",
    "import cv2\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ['CUDA_VISIBLE_DEVICES'] = '0'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load dataset\n",
    "folder = 'train'\n",
    "with open('D:/dataset/table/table_line/Split1/'+ folder+'_labels.json', 'r') as f:\n",
    "    labels = json.load(f)\n",
    "dataset = ImageDataset('D:/dataset/table/table_line/Split1/'+ folder+'_input', labels, 8, scale=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# init model\n",
    "net = SplitModel(3)\n",
    "net = torch.nn.DataParallel(net).cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "IncompatibleKeys(missing_keys=[], unexpected_keys=[])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load saved checkpoint\n",
    "net.load_state_dict(torch.load('split_model.pth'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DataParallel(\n",
       "  (module): SplitModel(\n",
       "    (sfcn): SFCN(\n",
       "      (conv1): Sequential(\n",
       "        (0): Conv2d(3, 18, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3), bias=False)\n",
       "        (1): ReLU(inplace)\n",
       "      )\n",
       "      (conv2): Sequential(\n",
       "        (0): Conv2d(18, 18, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3), bias=False)\n",
       "        (1): ReLU(inplace)\n",
       "      )\n",
       "      (conv3): Sequential(\n",
       "        (0): Conv2d(18, 18, kernel_size=(7, 7), stride=(1, 1), padding=(6, 6), dilation=(2, 2), bias=False)\n",
       "        (1): ReLU(inplace)\n",
       "      )\n",
       "    )\n",
       "    (rpn1): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(18, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(18, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(18, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(1, 2), stride=(1, 2), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (rpn2): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(1, 2), stride=(1, 2), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (rpn3): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(1, 2), stride=(1, 2), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0.3)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (rpn4): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(37, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(37, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(37, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(1, 2), stride=(1, 2), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (cpn1): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(18, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(18, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(18, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(2, 1), stride=(2, 1), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (cpn2): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(2, 1), stride=(2, 1), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (cpn3): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(36, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(2, 1), stride=(2, 1), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0.3)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (cpn4): ProjectionNet(\n",
       "      (conv_branch1): Sequential(\n",
       "        (0): Conv2d(37, 6, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch2): Sequential(\n",
       "        (0): Conv2d(37, 6, kernel_size=(3, 3), stride=(1, 1), padding=(3, 3), dilation=(3, 3))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (conv_branch3): Sequential(\n",
       "        (0): Conv2d(37, 6, kernel_size=(3, 3), stride=(1, 1), padding=(4, 4), dilation=(4, 4))\n",
       "        (1): GroupNorm(3, 6, eps=1e-05, affine=True)\n",
       "        (2): ReLU(inplace)\n",
       "      )\n",
       "      (project_module): ProjectionModule(\n",
       "        (max_pool): MaxPool2d(kernel_size=(2, 1), stride=(2, 1), padding=0, dilation=1, ceil_mode=False)\n",
       "        (feature_conv): Sequential(\n",
       "          (0): Conv2d(18, 18, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "          (1): GroupNorm(6, 18, eps=1e-05, affine=True)\n",
       "          (2): ReLU(inplace)\n",
       "        )\n",
       "        (prediction_conv): Sequential(\n",
       "          (0): Dropout2d(p=0)\n",
       "          (1): Conv2d(18, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        )\n",
       "        (feature_project): ProjectPooling()\n",
       "        (prediction_project): Sequential(\n",
       "          (0): ProjectPooling()\n",
       "          (1): Sigmoid()\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "  )\n",
       ")"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# change to eval mode\n",
    "net.eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "img, label = dataset[50]\n",
    "r,c = net(img.unsqueeze(0))\n",
    "r = r[-1]>0.5\n",
    "c = c[-1]>0.5\n",
    "c = c.cpu().detach().numpy()\n",
    "r = r.cpu().detach().numpy()\n",
    "r_im = r.reshape((-1,1))*np.ones((r.shape[0],c.shape[0]))\n",
    "c_im = c.reshape((1,-1))*np.ones((r.shape[0],c.shape[0]))\n",
    "im = cv2.bitwise_or(r_im,c_im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "Image.fromarray(img.numpy()[2]*255.).convert('L')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "Image.fromarray(im*255.).convert('L')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
